package com.feidee.fdhadoop.hive;

import com.feidee.fdcommon.configuration.CustomConfiguration;
import org.apache.log4j.Logger;

import java.sql.*;

public class HiveUtil {

	private static Logger logger = Logger.getLogger(HiveUtil.class);

	public static Connection getConn(String driver,String connUrl,String username,String pwd) {
		Connection con = null;
		try {
			Class.forName(driver);
//			KerberosAuthentication.authenticate();
			con = DriverManager.getConnection(connUrl, username, pwd);
		} catch (Exception e) {
			logger.error("Hive kerberos 获取连接失败", e);
		}
		return con;
	}

	public static void main(String[] args) {
		CustomConfiguration.setString("env", "product");
		try {
			System.out.println(HiveInitParam.getJDBCUrl());
			//Connection conn = HiveUtil.getConn("org.apache.hive.jdbc.HiveDriver",HiveInitParam.getJDBCUrl(),"guest","guest");
			Connection conn = HiveUtil.getConn("org.apache.hive.jdbc.HiveDriver",
					"jdbc:hive2://bj4-200-1-centos216,bj4-200-1-centos215,bj4-200-1-centos214,bj4-200-1-centos213,bj4-200-1-centos212/default;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2",
					"fdbd_rec","vbg56zxw");
			Statement stmt = conn.createStatement();
			//String sql = "select a from dwtest.test";
			String sql = "select * from temp.student";
			ResultSet res = stmt.executeQuery(sql);
			while (res.next()) {
				System.out.println(" return from HiveServer {}" + res.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
